package cn.wolfcode.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedList;

@Controller
@RequestMapping("/activity")
//控制器
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @RequiredPermission(name="活动显示",expression ="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name="活动删除",expression ="activity:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            activityService.delete(id);
        }
        LinkedList<Object> linkedList = new LinkedList<>();
        linkedList.add("1");
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="活动保存",expression ="activity:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Activity activity){
        if (activity.getId() != null) {
            activityService.update(activity);
        }else {
            activityService.save(activity);
        }
        return new JsonResult();
    }
}
